<script setup lang="ts">
import { useRoute, useRouter } from 'vue-router';
import { categoryGet } from '@/services';
import { goodsByTagtGet } from '@/services/home.ts';
import type { Category } from '@/types/home.ts';
import type { GoodsList } from '@/types';

const $route = useRoute();
const $router = useRouter();
const tagsCategoryList = ref<Category[]>([]);
const labelData = ref<any>({});
const categoryData = reactive({
  active: 0,
});
const onChange = () => {
  getTagsList();
};
const carPop = ref(false);
const joinCarData = ref({});
const onJoinCarClick = (props: GoodsList) => {
  carPop.value = true;
  joinCarData.value = props;
};

const onImageClick = (props: any) => {
  $router.push({
    path: `/goodsDetails/${props.goodsId}`,
  });
};
// 加载数据
const getTagsList = async () => {
  const { data, err } = await goodsByTagtGet({
    tagId: tagsCategoryList.value[categoryData.active].tagId,
  });
  if (err) {
    showToast(err);
    return;
  }
  labelData.value = data;
};

const loadData = async () => {
  const { data, err } = await categoryGet();
  if (err) {
    showToast(err);
    return;
  }
  tagsCategoryList.value = data.tagList;
  const index = data.tagList.findIndex((item: any) => item.tagId === $route.query.tagId);
  if (index !== -1) {
    categoryData.active = index;
  }
  // 获取该分类下的商品信息;
  await getTagsList();
};
onMounted(() => {
  loadData();
});
</script>

<template>
  <div class="container">
    <van-sidebar v-model="categoryData.active" @change="onChange">
      <van-sidebar-item
        v-for="category in tagsCategoryList"
        :key="category.id"
        :title="category.tagName"
      />
    </van-sidebar>
    <div class="content">
      <div class="tags-box" v-for="(value, key) in labelData" :key="key">
        <div class="box-title">{{ String(key).split('-')[1] }}</div>
        <div class="goods-box" v-for="goods in value" :key="goods.goodsId">
          <div class="box-left">
            <van-image @click="onImageClick(goods)" :src="goods.goodsImg?.split(',')[0]" />
          </div>
          <div class="box-right">
            <div class="right-text">
              <van-text-ellipsis rows="2" :content="goods.goodsName" />
            </div>
            <div class="right-bottom">
              <div class="price-text">
                <span class="right-price">￥</span>
                <span>{{ goods.goodsSalesPrice }}</span>
              </div>
              <svg-icon @click="onJoinCarClick(goods)" icon-class="car" size="1.5em" />
            </div>
          </div>
        </div>
      </div>
      <van-empty v-if="Object.keys(labelData).length === 0" description="暂无更多" />
    </div>
  </div>
  <JoinCar v-model="carPop" :joinCarData="joinCarData"></JoinCar>
</template>

<style lang="scss" scoped>
.container {
  display: flex;
  height: 100vh; /* 确保容器有固定高度 */
  overflow: hidden;

  .content {
    width: 100%;
    overflow: auto;
  }

  .tags-box {
    width: 100%;

    .box-title {
      height: 50px;
      width: 100%;
      font-size: 14px;
      font-weight: 500;
      padding-left: 8px;
      color: #222222;
      display: flex;
      align-items: center;
    }

    .goods-box {
      width: 100%;
      height: 88px;
      padding-left: 4px;
      margin-bottom: 12px;
      display: flex;

      .box-left {
        width: 80px;
        height: 100%;

        .van-image {
          width: 80px;
          height: 100%;
        }
      }

      .box-right {
        width: 100%;
        height: 100%;
        padding: 4px 0 4px 4px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

        .right-text {
          font-size: 13px;
          font-weight: 500;
          color: #000000;
        }

        .right-bottom {
          font-size: 14px;
          font-weight: 500;
          color: #e86600;
          height: 22px;
          padding-right: 16px;
          display: flex;
          align-items: center;
          justify-content: space-between;

          .price-text {
            .right-price {
              font-size: 10px;
              font-weight: 400;
            }
          }
        }
      }
    }
  }

  :deep(.van-sidebar) {
    position: relative;
    min-width: 88px !important;
    max-width: 88px !important;
    height: 100% !important;

    &::before {
      position: absolute;
      top: 50%;
      left: initial;
      right: 0;
      width: 1px;
      height: 100%;
      background-color: #ebebeb;
      transform: translateY(-50%);
      content: '';
    }
  }

  .van-sidebar-item {
    height: 60px;
    background: #fff;

    &::before {
      position: absolute;
      top: 50%;
      left: initial;
      right: 0;
      width: 1px;
      height: 100%;
      background-color: #ebebeb;
      transform: translateY(-50%);
      content: '';
    }
  }

  .van-sidebar-item--select {
    color: #00ba9f;

    &::before {
      position: absolute;
      width: 1.5px;
      height: 100%;
      top: 50%;
      right: 0;
      left: initial;
      background-color: #00ba9f;
      transform: translateY(-50%);
      content: '';
    }
  }
}
</style>
